#!/bin/bash
set -e

lint_ignore() {
    while read line; do
        fileName=$(echo $line | cut -d':' -f1 )
        lineNumber=$(echo $line | cut -d':' -f2 )
        match=$(grep -n golint_ignore $fileName | grep -e "^${lineNumber}:")
        if [ -z "$match" ]; then
            echo $line
        fi
    done
}


cd $(dirname $0)/..

echo Running validation

PACKAGES=". $(find -name '*.go' | xargs -I{} dirname {} |  cut -f2 -d/ | sort -u | grep -Ev '(^\.$|.git|.trash-cache|vendor|bin)' | sed -e 's!^!./!' -e 's!$!/...!')"

echo Running: go vet
go vet ${PACKAGES}
echo Running: golint
for i in ${PACKAGES}; do
    if [ -n "$(golint $i | grep -v 'should have comment.*or be unexported' | grep -v 'method ById should be ByID' | lint_ignore | tee /dev/stderr)" ]; then
        failed=true
    fi
done
test -z "$failed"
echo Running: go fmt
test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)"
